<?php
// +----------------------------------------------------------------------
// | 麦沃德科技赋能开发者，助力中小企业发展
// +----------------------------------------------------------------------
// | Copyright (c) 2017～2024  www.wdadmin.cn    All rights reserved.
// +----------------------------------------------------------------------
// | Wdadmin系统产品软件并不是自由软件，不加密，并不代表开源，未经许可不可自由转售和商用
// +----------------------------------------------------------------------
// | Author: MY WORLD Team <bd@maiwd.cn>   www.wdadmin.cn
// +----------------------------------------------------------------------
namespace app\api\controller\wddj\driver;

use app\api\model\wddj\User;
use app\common\controller\Api;
use think\Db;
use Exception;
use think\exception\PDOException;
use think\exception\ValidateException;

/**
 * Class Driver
 * Desc  代驾控制器
 * Create on 2024/10/22 15:11
 * Create by wangyafang
 */
class Driver extends Api
{
    protected $noNeedLogin = [''];
    protected $noNeedRight = ['center','operate','update_lng_lat'];
    protected $model = null;
    protected $driverObj = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\api\model\wddj\driver\Order();
        $wechat_id = User::getWechatId($this->auth->id);
        $driverObj = (new \app\api\model\wddj\driver\Driver())->where('wechat_id',$wechat_id)->find();
        if (!$driverObj) {
            $this->error('不是司机，没有权限,请先认证');
        }
        $this->driverObj = $driverObj;
    }
    
    /**
     * Desc 代驾中心
     * Create on 2024/10/22 15:12
     * Create by wangyafang
     */
    public function center()
    {
        if(!$this->request->isGet()) {
            $this->error('请求类型错误');
        }

        $data['order_status'] = $this->driverObj->order_status;

        $today_acceptance_number = (new \app\api\model\wddj\driver\Order())
            ->where('state','in',['accepted','in_progress','adjusting','refunded','awaiting_payment','completed'])
            ->where('driver_id',$this->driverObj->id)
            ->where('driver_wechat_id',$this->driverObj->wechat_id)
            ->whereTime('acceptance_time', 'today')
            ->count();
        $today_income = (new \app\api\model\wddj\driver\IncomeExpenditure())
            ->where('driver_id',$this->driverObj->id)
            ->where('wechat_id',$this->driverObj->wechat_id)
            ->whereTime('createtime', 'today')
            ->where('operate','1')
            ->where('group_type','1')
            ->sum('amount');
        $data['statistics'] = array(
            'today_acceptance_number'=>$today_acceptance_number,
            'today_income'=>$today_income,
        );
        $this->success('请求成功',$data);
    }
    
    /**
     * Desc 操作(开始接单或者修改)
     * Create on 2024/10/22 15:13
     * Create by wangyafang
     */
    public function operate()
    {
        $this->success('操作成功');
        if(!$this->request->isPost()) {
            $this->error('请求类型错误');
        }
        $order_status = $this->request->post('order_status');
        if ($order_status == 2) {
            $driver_acceptance_number = (new \app\api\model\wddj\driver\Order())
                ->where('state','in',['accepted','in_progress'])
                ->where('driver_id',$this->driverObj->id)
                ->where('driver_wechat_id',$this->driverObj->wechat_id)
                ->count();
            if ($driver_acceptance_number > 0) {
                $this->error('有订单没有完成,不能休息');
            }
        }
        Db::startTrans();
        try {
            $this->driverObj->save(['order_status'=>$order_status]);
            Db::commit();
        } catch (ValidateException|PDOException|Exception $e) {
            Db::rollback();
            $this->error($e->getMessage());
        }

        $this->success('操作成功');
    }
    
    /**
     * Desc 更新司机经度纬度
     * Create on 2024/10/30 16:56
     * Create by wangyafang
     */
    public function update_lng_lat()
    {
        $this->success('更新成功');
        if(!$this->request->isPost()) {
            $this->error('请求类型错误');
        }
        $param = $this->request->post();
        $result = $this->validate($param,'app\api\validate\wddj\driver\Driver.update_lng_lat');
        if(true !== $result){
            // 验证失败 输出错误信息
            $this->error($result);
        }
        Db::startTrans();
        try {
            $this->driverObj->save($param);
            Db::commit();
        } catch (ValidateException|PDOException|Exception $e) {
            Db::rollback();
            $this->error($e->getMessage());
        }

        $this->success('更新成功');
    }
}



 